{include file="pub:header"/}
<script type="text/javascript" src="/static/admin/js/listTable.js?c=508"></script>
</head>
<body>

<div class="subnav">
    <div class="content_menu ib_a blue line_x">
        <a href="javascript:;"><span class="pagetitle">【备份数据库】</span></a>
        <a href="{:url('database/index',array('type'=>'export'))}" class="on"><em>备份数据库</em></a>
        <span>|</span><a href="{:url('database/index',array('type'=>'import'))}" class=""><em>下载备份数据</em></a>

	</div>
</div>

	<div class="pad_lr_10">

			<table width="100%" cellspacing="0" class="search_form">
				<tbody>
					<tr>
						<td>
							<div class="explain_col">
						        <a id="export" href="javascript:;" autocomplete="off">立即备份</a>&nbsp;&nbsp;
						        <a id="optimize" href="{:url('optimize')}">优化表</a>&nbsp;&nbsp;
						        <a id="repair" href="{:url('repair')}">修复表</a>
							</div>
						</td>
					</tr>
				</tbody>
			</table>


		<div class="J_tablelist table_list">

         <form id="export-form" method="post" action="{:url('export')}">

			<table width="100%" cellspacing="0">
				<thead>
					<tr>
						<th><input type="checkbox" checked="chedked" id="checkall_t" class="J_checkall"></th>
						<th align="left">表名</th>
						<th>数据量</th>
						<th>数据大小</th>
						<th>创建时间</th>
						<th>备份状态</th>
						<th>操作</th>
					</tr>
				</thead>


				<tbody>

		         {volist name="list" id="table"}

                        <tr>
                            <td align="center">
                                <input checked="chedked"  class="J_checkitem" type="checkbox" name="tables[]" value="{$table.name}">
                            </td>
                            <td>{$table.name}</td>
                            <td align="center">{$table.rows}</td>
                            <td align="center">{$table.data_length|format_bytes}</td>
                            <td align="center">{$table.create_time}</td>
                            <td align="center" class="info">未备份</td>
                            <td align="center">
                                【<a href="javascript:void(0);" onclick="ajaxget('{:url(\'optimize?tables=\'.$table[\'name\'])}','是否优化表 ？')" >优化表</a>】
                                【<a href="javascript:void(0);" onclick="ajaxget('{:url(\'repair?tables=\'.$table[\'name\'])}','是否修复表 ？')">修复表</a>】
                            </td>
                        </tr>
		           {/volist}

				</tbody>

			</table>

		  </form>

		</div>

	</div>


<script type="text/javascript">
    (function($){
        var $form = $("#export-form"), $export = $("#export"), tables
            $optimize = $("#optimize"), $repair = $("#repair");

        $('.J_tablelist').listTable();

        $optimize.add($repair).click(function(){
            $.post(this.href, $form.serialize(), function(data){
                if(data.code){
                    alert(data.msg);
                } else {
                    alert(data.msg);
                }
            }, "json");
            return false;
        });

        $export.click(function(){
            //$export.parent().children().addClass("disabled");
            $export.html("正在发送备份请求...");
            $.post(
                $form.attr("action"),
                $form.serialize(),
                function(data){
                    if(data.code){
                        tables = data.data.tables;
                        $export.html(data.msg + "开始备份，请不要关闭本页面！");
                        backup(data.data.tab);
                        window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
                    } else {
                        alert(data.msg);
                        $export.parent().children().removeClass("disabled");
                        $export.html("立即备份");
                    }
                },
                "json"
            );
            return false;
        });

        function backup(tab, status){
            status && showmsg(tab.id, "开始备份...(0%)");
            $.get($form.attr("action"), tab, function(data){
                if(data.code){
                    showmsg(tab.id, data.msg);

                    if(!$.isPlainObject(data.data.tab)){
                        $export.parent().children().removeClass("disabled");
                        $export.html("备份完成，点击重新备份");
                        window.onbeforeunload = function(){ return null }
                        return;
                    }
                    backup(data.data.tab, tab.id != data.data.tab.id);
                } else {
                    alert(data.msg);
                    $export.parent().children().removeClass("disabled");
                    $export.html("立即备份");
                }
            }, "json");

        }

        function showmsg(id, msg){
            $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
        }
    })(jQuery);
</script>

{include file="pub:footer" /}
